package com.yy.wga.merchant.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.core.dto.PageBean;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.util.mapping.BeansUtils;
import com.yy.wga.merchant.feign.request.MerchantMomentShareGetReq;
import com.yy.wga.merchant.feign.request.MerchantMomentSharePageReq;
import com.yy.wga.merchant.feign.request.MerchantMomentShareSaveReq;
import com.yy.wga.merchant.feign.response.MerchantMomentShareGetResp;
import com.yy.wga.merchant.feign.response.MerchantMomentSharePageResp;
import com.yy.wga.merchant.service.mapper.MerchantMomentShareMapper;
import com.yy.wga.merchant.service.pojo.MerchantMomentShareDO;
import com.yy.wga.merchant.service.pojo.MerchantMomentShareImgDO;
import com.yy.wga.merchant.service.service.MerchantMomentShareImgService;
import com.yy.wga.merchant.service.service.MerchantMomentShareService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 微信朋友圈分享素材(ResourceMomentShare)表服务实现类
 *
 * @author guochao
 * @since 2024-10-31 22:12:53
 */
@Service
@RequiredArgsConstructor
public class ResourceMomentShareServiceImpl extends ServiceImpl<MerchantMomentShareMapper, MerchantMomentShareDO> implements MerchantMomentShareService {
    private final MerchantMomentShareImgService momentShareImgService;

    @Override
    public PageBean<MerchantMomentSharePageResp> pageData(MerchantMomentSharePageReq request) {
        LambdaQueryWrapper<MerchantMomentShareDO> queryWrapper = new LambdaQueryWrapper<MerchantMomentShareDO>()
                .eq(MerchantMomentShareDO::getAppKey, request.getAppKey())
                .like(StringUtils.isNotBlank(request.getContent()), MerchantMomentShareDO::getContent, request.getContent())
                .eq(StringUtils.isNotBlank(request.getSpuId()), MerchantMomentShareDO::getSpuId, request.getSpuId())
                .gt(request.getStartTime() != null, MerchantMomentShareDO::getCreateTime, request.getStartTime())
                .lt(request.getEndTime() != null, MerchantMomentShareDO::getCreateTime, request.getEndTime())
                .orderByDesc(MerchantMomentShareDO::getTop)
                .orderByDesc(MerchantMomentShareDO::getSort)
                .orderByDesc(MerchantMomentShareDO::getId);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<MerchantMomentShareDO> shares = baseMapper.selectList(queryWrapper);
        List<MerchantMomentSharePageResp> responseList = BeansUtils.mapList(shares, MerchantMomentSharePageResp.class);
        if (!CollectionUtils.isEmpty(responseList)) {
            Set<Long> shareIds = responseList.stream()
                    .map(MerchantMomentSharePageResp::getId)
                    .collect(Collectors.toSet());
            // 查询图片
            List<MerchantMomentShareImgDO> imgList = momentShareImgService.list(new LambdaQueryWrapper<MerchantMomentShareImgDO>()
                    .eq(MerchantMomentShareImgDO::getAppKey, request.getAppKey())
                    .in(MerchantMomentShareImgDO::getShareId, shareIds));
            if (!CollectionUtils.isEmpty(imgList)) {
                // 填充图片
                Map<Long, List<MerchantMomentShareImgDO>> imgMap = imgList.stream()
                        .collect(Collectors.groupingBy(MerchantMomentShareImgDO::getShareId));
                for (MerchantMomentSharePageResp response : responseList) {
                    List<MerchantMomentShareImgDO> imgs = imgMap.getOrDefault(response.getId(), Collections.emptyList());
                    response.setImgUrls(imgs.stream()
                            .map(MerchantMomentShareImgDO::getImgUrl).collect(Collectors.toList()));
                }
            }

        }
        PageBean<MerchantMomentSharePageResp> pageBean = new PageBean<>();
        pageBean.setPageInfo(shares);
        pageBean.setList(responseList);
        return pageBean;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public void saveData(MerchantMomentShareSaveReq request) {
        MerchantMomentShareDO share = BeansUtils.map(request, MerchantMomentShareDO.class);
        if (share.getId() == null) {
            share.setAppKey(request.getAppKey());
            baseMapper.insert(share);
        } else {
            baseMapper.updateById(share);

            momentShareImgService.remove(new LambdaQueryWrapper<MerchantMomentShareImgDO>()
                    .eq(MerchantMomentShareImgDO::getAppKey, request.getAppKey())
                    .eq(MerchantMomentShareImgDO::getShareId, share.getId()));
        }

        List<MerchantMomentShareImgDO> shareImgs = new ArrayList<>(request.getImgUrls().size());
        for (String imgUrl : request.getImgUrls()) {
            MerchantMomentShareImgDO shareImg = new MerchantMomentShareImgDO();
            shareImg.setAppKey(request.getAppKey());
            shareImg.setShareId(share.getId());
            shareImg.setImgUrl(imgUrl);
            shareImgs.add(shareImg);
        }
        momentShareImgService.saveBatch(shareImgs);
    }

    @Override
    public List<MerchantMomentShareGetResp> get(MerchantMomentShareGetReq request) {
        List<MerchantMomentShareDO> merchantMomentShareList = baseMapper.selectList(new LambdaQueryWrapper<MerchantMomentShareDO>()
                .eq(MerchantMomentShareDO::getAppKey, request.getAppKey())
                .eq(MerchantMomentShareDO::getSpuId, request.getSpuId())
                .eq(MerchantMomentShareDO::getStatus, 1)
                .orderByDesc(MerchantMomentShareDO::getTop)
                .orderByDesc(MerchantMomentShareDO::getSort)
                .orderByDesc(MerchantMomentShareDO::getId));
        if (CollectionUtils.isEmpty(merchantMomentShareList)) {
            return Collections.emptyList();
        }

        Set<Long> shareIds = merchantMomentShareList.stream()
                .map(MerchantMomentShareDO::getId)
                .collect(Collectors.toSet());
        // 查询图片
        List<MerchantMomentShareImgDO> imgList = momentShareImgService.list(new LambdaQueryWrapper<MerchantMomentShareImgDO>()
                .eq(MerchantMomentShareImgDO::getAppKey, request.getAppKey())
                .in(MerchantMomentShareImgDO::getShareId, shareIds));
        if (CollectionUtils.isEmpty(imgList)) {
            imgList = Collections.emptyList();
        }
        List<MerchantMomentShareGetResp> responseList = new ArrayList<>(merchantMomentShareList.size());
        // 填充图片
        Map<Long, List<MerchantMomentShareImgDO>> imgMap = imgList.stream()
                .collect(Collectors.groupingBy(MerchantMomentShareImgDO::getShareId));
        for (MerchantMomentShareDO share : merchantMomentShareList) {
            List<MerchantMomentShareImgDO> imgs = imgMap.getOrDefault(share.getId(), Collections.emptyList());
            MerchantMomentShareGetResp response = new MerchantMomentShareGetResp();
            response.setContent(share.getContent());
            response.setImgUrls(imgs.stream()
                    .map(MerchantMomentShareImgDO::getImgUrl).collect(Collectors.toList()));
            responseList.add(response);
        }
        return responseList;
    }
}
